package com.nike.amoeba.ui.frg

import android.os.Bundle
import android.view.View
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.Observer
import androidx.lifecycle.viewModelScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ConvertUtils
import com.blankj.utilcode.util.LogUtils
import com.google.gson.Gson
import com.kingja.loadsir.core.LoadService
import com.nike.amoeba.R
import com.nike.amoeba.adapter.StockRuAdapter
import com.nike.amoeba.adapter.StockStorageAdapter
import com.nike.amoeba.base.BaseFragment
import com.nike.amoeba.bean.DataBean
import com.nike.amoeba.databinding.FStockRuEpcBinding
import com.nike.amoeba.ext.init
import com.nike.amoeba.ext.loadListData
import com.nike.amoeba.ext.loadServiceInit
import com.nike.amoeba.ext.showEmpty
import com.nike.amoeba.ext.showLoading
import com.nike.amoeba.mar.eventViewModel
import com.nike.amoeba.viewmodel.AssetModel
import com.nike.amoeba.viewmodel.RfidModel
import com.nike.amoeba.viewmodel.StockManageModel
import com.nike.amoeba.weight.recyclerview.SpaceItemDecoration
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

/**
 * User: Nike
 *  2024/5/13 09:49
 *      入库存盘点
 */
class StockRuEpcFrg: BaseFragment<StockManageModel, FStockRuEpcBinding>(){

    val adapter: StockRuAdapter by lazy { StockRuAdapter(arrayListOf()) }

    val storageUnitSpinnerAdapter: StockStorageAdapter by lazy { StockStorageAdapter(requireContext(), mViewModel.storageUnitListBean) }

    var stockType: Int = 0

    var bean: DataBean? = null

    var stockId: String? = null

    lateinit var loadsir: LoadService<Any>

    val stockManageModel: StockManageModel by activityViewModels()

    override fun initView(savedInstanceState: Bundle?) {
        arguments?.let {
            mViewModel.batchNo.set(it.getString("batchNo"))
            mViewModel.code.set(it.getString("code"))
            stockType = it.getInt("stockType")
            bean = Gson().fromJson(it.getString("bean") , DataBean::class.java)
            stockId = bean!!.id
        }
        mDatabind.viewmodel = mViewModel
        mDatabind.click = ProxyClick()

        //状态页配置
        loadsir = loadServiceInit(mDatabind.swipeRefresh) {
            loadsir.showLoading()
            mViewModel.onRequest(bean!!)
        }

        mViewModel.viewModelScope.launch(Dispatchers.IO) {
            val configBean = mViewModel.configDao.findFirst()
            configBean.powerType = 0
            mViewModel.configDao.update(configBean)
        }
        mViewModel.save.set(getString(R.string.save))
        setToolBarTitle(getString(R.string.number) + "：" + bean!!.AssetNo!!)

        mDatabind.swipeRefresh.isEnabled = false
        mDatabind.recyclerView.init(LinearLayoutManager(context), adapter).let {
            it.addItemDecoration(SpaceItemDecoration(ConvertUtils.dp2px(20f), ConvertUtils.dp2px(20f), true))
        }

        adapter.run {
            setOnItemClickListener{adapter, view, position ->
                val bean = adapter.data[position] as DataBean
                if (bean.isCheckedSave)return@setOnItemClickListener
                bean.Type = if (bean.Type == 0) 1 else 0
                setData(position, bean)
            }
        }

        mViewModel.onRequest(bean!!)
    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.listBean.observe(viewLifecycleOwner, Observer {
            loadListData(it, adapter, loadsir, mDatabind.recyclerView, mDatabind.swipeRefresh, it.pageSize)
            adapter.appendList(it.listData)
            stockManageModel.updateStockManageListCallback.value = true
        })
        mViewModel.storageUnitList.observe(viewLifecycleOwner, {
            mDatabind.spAllStorage.adapter = storageUnitSpinnerAdapter
            mDatabind.spAllStorage.visibility = View.VISIBLE
            mDatabind.lyStorage.visibility = View.VISIBLE
        })
    }

    inner class ProxyClick() {
        fun save(){
            if (adapter.mFilterList.size == 0)return
//            val storageUnitBean = mViewModel.storageUnitListBean[mDatabind.spAllStorage.selectedItemPosition]
            mViewModel.onSave(null, stockId, adapter.mFilterList, this@StockRuEpcFrg)
        }
    }
}